//package systempen.cmanager;
//public class Del {
//}
package systempen.cmanager;

import java.util.Calendar;

import systempen.cmanager.DialogTypeBrowser.IFoundCostType;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class Del extends AcitivityLowImportance {
	Activity thisclass;
	public TextView textFrom;
	public TextView textTo;
	public TextView textCost;
	String level;
	static final int DATE_DIALOG_ID_from = 0;
	static final int DATE_DIALOG_ID_to = 1;
	String foundedType;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        try{
	        super.onCreate(savedInstanceState);
	        ThemePicker.onActivityCreateSetTheme(this);
	        setContentView(R.layout.del);
	        thisclass = this;
	        textFrom = (TextView) findViewById(R.id.txtFrom);
	        textTo = (TextView) findViewById(R.id.txtTo);
	        textCost = (TextView) findViewById(R.id.txtTypeName);
	        level ="0";
	        Calendar c = Calendar.getInstance();
	    	int cyear = c.get(Calendar.YEAR);
	    	int cmonth = c.get(Calendar.MONTH);
	    	int cday = c.get(Calendar.DAY_OF_MONTH);
	    	String ccmonth,ccday,ccyear;
	    	if (cmonth < 10){
	    		ccmonth = "0"+String.valueOf(cmonth+1);
	    	}else
	    		ccmonth = String.valueOf(cmonth+1);
	    	if (cday < 10){
	    		ccday = "0"+String.valueOf(cday);
	    	}else
	    		ccday = String.valueOf(cday);
	    	ccyear = String.valueOf(cyear);
	    	String date_selected = ccyear+"-"+ccmonth+"-"+ccday;
	    	textFrom.setText(date_selected);
	    	textTo.setText(date_selected);
	    	textFrom.setText(util.getDefaultFromDateView(getApplicationContext()));
	    	textTo.setText(util.getDefaultToDateView(getApplicationContext()));

	    	

        } catch(Exception ex){ 
        	util.showDialog(ex.getMessage().toString(), thisclass);
        }
    }
    public void onRunReport (View view){
        try{
			String from,to;
			from = util.viewStringDateToDBStringDate(textFrom.getText().toString(),getApplicationContext());
			to = util.viewStringDateToDBStringDate(textTo.getText().toString(),getApplicationContext());
			//ovde dodati, brisanje!!!
			//////////////////////////////scary alert builder dialog
			AlertDialog.Builder alt_bld = new AlertDialog.Builder(Del.this);
			alt_bld.setMessage("Are you sure you want to delete costs?")
			.setCancelable(false)
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
			// Action for 'Yes' Button
				try{
					//util.execSQL("delete from log where a = "+util.getBracketValue((String) ((TextView) arg).getText()), thisclass);
					String deleteSQL="";
					String podtipovi = util.getAllChildren(level, thisclass);
					deleteSQL = "delete from log where whe between '"+util.viewStringDateToDBStringDate(textFrom.getText().toString(),getApplicationContext())+"' and '"+util.viewStringDateToDBStringDate(textTo.getText().toString(),getApplicationContext())+"'"+
					" and cost_code in ("+podtipovi+")";
					String t = deleteSQL;
					util.execSQL(t, thisclass);
				}
				catch(Exception ex)
				{
					util.showDialog(ex.getMessage(),thisclass);
				}
			}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
			//  Action for 'NO' Button
			dialog.cancel();
			}
			});
			AlertDialog alert = alt_bld.create();
			// Title for AlertDialog
			alert.setTitle("Delete costs?");
			// Icon for AlertDialog
			alert.setIcon(R.drawable.help);
			alert.show();
			//////////////////////////////end scary alert builder dialog

			//kraj dodati brisanje
        } catch(Exception ex){
        	util.showDialog(ex.getMessage().toString(), thisclass);
        }
    }
	@Override //pressed back button cya
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	    		startActivity(new Intent(thisclass, ReportList.class)); 
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
    public void onChooseType (View view){
    	IFoundCostType ifo = new IFoundCostType() {
			
			@Override
			public void onFoundCostType(String costType) {
				level = costType;
				foundedType = util.getLevelName(costType, getApplicationContext());
				textCost.setText(foundedType); 
			} 
		};
    	DialogTypeBrowser dt = new DialogTypeBrowser(this, "0", ifo);
    	dt.show();
    }
    
    public void onChangeFromDate (View view){
        try{
        	showDialog(DATE_DIALOG_ID_from);
        } catch(Exception ex){
        	util.showDialog(ex.getMessage().toString(), thisclass);
        }
    }
    public void onChangeToDate (View view){
        try{
        	showDialog(DATE_DIALOG_ID_to);
        } catch(Exception ex){
        	util.showDialog(ex.getMessage().toString(), thisclass);
        }
    }

    	// Creating dialog
    	@Override
    protected Dialog onCreateDialog(int id) {
	    	Calendar c = Calendar.getInstance();
	    	int cyear = c.get(Calendar.YEAR);
	    	int cmonth = c.get(Calendar.MONTH);
	    	int cday = c.get(Calendar.DAY_OF_MONTH);
	    	switch (id) {
	    	case DATE_DIALOG_ID_from:
	    		return new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
	    	case DATE_DIALOG_ID_to:
	    		return new DatePickerDialog(this,  mDateSetListener2,  cyear, cmonth, cday);
	    	}
	    	return null;
    	}
    	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    		// onDateSet method
    		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
	   			try{
		    		//String date_selected = String.valueOf(monthOfYear+1)+" /"+String.valueOf(dayOfMonth)+" /"+String.valueOf(year);
		    		String date_selected = String.valueOf(year)+"-"+util.AddZeros(monthOfYear+1)+"-"+util.AddZeros(dayOfMonth);
	    		
		    		Toast.makeText(Del.this, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
		    		textFrom.setText(util.dbStringDateToViewStringDate(date_selected, getApplicationContext()));
	    		}
	    		catch(Exception ex){
	            	util.showDialog(ex.getMessage().toString(), thisclass);
	            }	
    		}
    	};
    	private DatePickerDialog.OnDateSetListener mDateSetListener2 = new DatePickerDialog.OnDateSetListener() {
    		// onDateSet method
    		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
	   			try{
	   				String date_selected = String.valueOf(year)+"-"+util.AddZeros(monthOfYear+1)+"-"+util.AddZeros(dayOfMonth);
		    		Toast.makeText(Del.this, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
		    		textTo.setText(util.dbStringDateToViewStringDate(date_selected, getApplicationContext()));
	    		}
	    		catch(Exception ex){
	            	util.showDialog(ex.getMessage().toString(), thisclass);
	            }	
    		}
    	};
}


